(function() {
	var iOSCheckbox;
	iOSCheckbox = (function() {
		function iOSCheckbox(elem, options) {
			var key, opts, value;
			this.elem = $(elem);
			opts = $.extend({}, iOSCheckbox.defaults, options);
			for (key in opts) {
				value = opts[key];
				this[key] = value;
			}
			this.wrapCheckboxWithDivs();
			this.attachEvents();
			this.disableTextSelection();
			if (this.resizeHandle) {
				this.optionallyResize('handle');
			}
			if (this.resizeContainer) {
				this.optionallyResize('container');
			}
			this.initialPosition();
		}
		iOSCheckbox.prototype.isDisabled = function() {
			return this.elem.is(':disabled');
		};
		iOSCheckbox.prototype.wrapCheckboxWithDivs = function() {
			this.elem.wrap("<div class='" + this.containerClass + "' />");
			this.container = this.elem.parent();
			this.offLabel = $(
					"<label class='" + this.labelOffClass + "'>\n  <span>"
							+ this.uncheckedLabel + "</span>\n</label>")
					.appendTo(this.container);
			this.offSpan = this.offLabel.children('span');
			this.onLabel = $(
					"<label class='" + this.labelOnClass + "'>\n  <span>"
							+ this.checkedLabel + "</span>\n</label>")
					.appendTo(this.container);
			this.onSpan = this.onLabel.children('span');
			return this.handle = $(
					"<div class='" + this.handleClass + "'>\n  <div class='"
							+ this.handleRightClass + "'>\n    <div class='"
							+ this.handleCenterClass + "' />\n  </div>\n</div>")
					.appendTo(this.container);
		};
		iOSCheckbox.prototype.disableTextSelection = function() {
			if ($.browser.msie) {
				return $(
						[ this.handle, this.offLabel, this.onLabel,
								this.container ]).attr("unselectable", "on");
			}
		};
		iOSCheckbox.prototype.optionallyResize = function(mode) {
			var newWidth, offLabelWidth, onLabelWidth;
			onLabelWidth = this.onLabel.width();
			offLabelWidth = this.offLabel.width();
			if (mode === "container") {
				newWidth = onLabelWidth > offLabelWidth ? onLabelWidth
						: offLabelWidth;
				newWidth += this.handle.width() + this.handleMargin;
				return this.container.css({
					width : newWidth
				});
			} else {
				newWidth = onLabelWidth > offLabelWidth ? onLabelWidth
						: offLabelWidth;
				return this.handle.css({
					width : newWidth
				});
			}
		};
		iOSCheckbox.prototype.onMouseDown = function(event) {
			var x;
			event.preventDefault();
			if (this.isDisabled()) {
				return;
			}
			x = event.pageX || event.originalEvent.changedTouches[0].pageX;
			iOSCheckbox.currentlyClicking = this.handle;
			iOSCheckbox.dragStartPosition = x;
			return iOSCheckbox.handleLeftOffset = parseInt(this.handle
					.css('left'), 10) || 0;
		};
		iOSCheckbox.prototype.onDragMove = function(event, x) {
			var newWidth, p;
			if (iOSCheckbox.currentlyClicking !== this.handle) {
				return;
			}
			p = (x + iOSCheckbox.handleLeftOffset - iOSCheckbox.dragStartPosition)
					/ this.rightSide;
			if (p < 0) {
				p = 0;
			}
			if (p > 1) {
				p = 1;
			}
			newWidth = p * this.rightSide;
			this.handle.css({
				left : newWidth
			});
			this.onLabel.css({
				width : newWidth + this.handleRadius
			});
			this.offSpan.css({
				marginRight : -newWidth
			});
			return this.onSpan.css({
				marginLeft : -(1 - p) * this.rightSide
			});
		};
		iOSCheckbox.prototype.onDragEnd = function(event, x) {
			var p;
			if (iOSCheckbox.currentlyClicking !== this.handle) {
				return;
			}
			if (this.isDisabled()) {
				return;
			}
			if (iOSCheckbox.dragging) {
				p = (x - iOSCheckbox.dragStartPosition) / this.rightSide;
				this.elem.prop('checked', p >= 0.5);
			} else {
				this.elem.prop('checked', !this.elem.prop('checked'));
			}
			iOSCheckbox.currentlyClicking = null;
			iOSCheckbox.dragging = null;
			return this.elem.change();
		};
		iOSCheckbox.prototype.onChange = function() {
			var new_left;
			if (this.isDisabled()) {
				this.container.addClass(this.disabledClass);
				return false;
			} else {
				this.container.removeClass(this.disabledClass);
			}
			if (typeof (beforeChangeCheckbox) == "function") {
				var result = beforeChangeCheckbox();
				if (!result)
					return false;
			}
			new_left = this.elem.prop('checked') ? this.rightSide : 0;
			this.handle.animate({
				left : new_left
			}, this.duration);
			this.onLabel.animate({
				width : new_left + this.handleRadius
			}, this.duration);
			this.offSpan.animate({
				marginRight : -new_left
			}, this.duration);
			return this.onSpan.animate({
				marginLeft : new_left - this.rightSide
			}, this.duration);
		};
		iOSCheckbox.prototype.attachEvents = function() {
			var localMouseMove, localMouseUp, self;
			self = this;
			localMouseMove = function(event) {
				return self.onGlobalMove.apply(self, arguments);
			};
			localMouseUp = function(event) {
				self.onGlobalUp.apply(self, arguments);
				$(document).unbind('mousemove touchmove', localMouseMove);
				return $(document).unbind('mouseup touchend', localMouseUp);
			};
			this.container.bind('mousedown touchstart', function(event) {
				self.onMouseDown.apply(self, arguments);
				$(document).bind('mousemove touchmove', localMouseMove);
				return $(document).bind('mouseup touchend', localMouseUp);
			});
			return this.elem.bind("change", function() {
				return self.onChange.apply(self, arguments);
			});
		};
		iOSCheckbox.prototype.initialPosition = function() {
			var offset;
			this.offLabel.css({
				width : this.container.width() - this.containerRadius
			});
			offset = this.containerRadius + 1;
			if ($.browser.msie && $.browser.version < 7) {
				offset -= 3;
			}
			this.rightSide = this.container.width() - this.handle.width()
					- offset;
			if (this.elem.is(':checked')) {
				this.handle.css({
					left : this.rightSide
				});
				this.onLabel.css({
					width : this.rightSide + this.handleRadius
				});
				this.offSpan.css({
					marginRight : -this.rightSide
				});
			} else {
				this.onLabel.css({
					width : 0
				});
				this.onSpan.css({
					marginLeft : -this.rightSide
				});
			}
			if (this.isDisabled()) {
				return this.container.addClass(this.disabledClass);
			}
		};
		iOSCheckbox.prototype.onGlobalMove = function(event) {
			var x;
			if (!(!this.isDisabled() && iOSCheckbox.currentlyClicking)) {
				return;
			}
			event.preventDefault();
			x = event.pageX || event.originalEvent.changedTouches[0].pageX;
			if (!iOSCheckbox.dragging
					&& (Math.abs(iOSCheckbox.dragStartPosition - x) > this.dragThreshold)) {
				iOSCheckbox.dragging = true;
			}
			return this.onDragMove(event, x);
		};
		iOSCheckbox.prototype.onGlobalUp = function(event) {
			var x;
			if (!iOSCheckbox.currentlyClicking) {
				return;
			}
			event.preventDefault();
			x = event.pageX || event.originalEvent.changedTouches[0].pageX;
			return this.onDragEnd(event, x);
		};
		iOSCheckbox.defaults = {
			duration : 200,
			checkedLabel : 'ON',
			uncheckedLabel : 'OFF',
			resizeHandle : true,
			resizeContainer : true,
			disabledClass : 'iPhoneCheckDisabled',
			containerClass : 'iPhoneCheckContainer',
			labelOnClass : 'iPhoneCheckLabelOn',
			labelOffClass : 'iPhoneCheckLabelOff',
			handleClass : 'iPhoneCheckHandle',
			handleCenterClass : 'iPhoneCheckHandleCenter',
			handleRightClass : 'iPhoneCheckHandleRight',
			dragThreshold : 5,
			handleMargin : 15,
			handleRadius : 4,
			containerRadius : 5
		};
		return iOSCheckbox;
	})();
	$.iphoneStyle = this.iOSCheckbox = iOSCheckbox;
	$.fn.iphoneStyle = function(options) {
		var checkbox, _i, _len, _ref;
		_ref = this.filter(':checkbox');
		for (_i = 0, _len = _ref.length; _i < _len; _i++) {
			checkbox = _ref[_i];
			$(checkbox).data("iphoneStyle", new iOSCheckbox(checkbox, options));
		}
		return this;
	};
	$.fn.iOSCheckbox = function(options) {
		var checkbox, opts, _i, _len, _ref;
		if (options == null) {
			options = {};
		}
		opts = $.extend({}, options, {
			resizeHandle : false,
			disabledClass : 'iOSCheckDisabled',
			containerClass : 'iOSCheckContainer',
			labelOnClass : 'iOSCheckLabelOn',
			labelOffClass : 'iOSCheckLabelOff',
			handleClass : 'iOSCheckHandle',
			handleCenterClass : 'iOSCheckHandleCenter',
			handleRightClass : 'iOSCheckHandleRight'
		});
		_ref = this.filter(':checkbox');
		for (_i = 0, _len = _ref.length; _i < _len; _i++) {
			checkbox = _ref[_i];
			$(checkbox).data("iOSCheckbox", new iOSCheckbox(checkbox, opts));
		}
		return this;
	};
}).call(this);
